#encoding=utf8
'''
Created on Oct 29, 2014

@author: jingyang <jingyang@smalltreemedia.com>
'''
from django.db import models
from django import forms

from core.widgets import CustomTextarea


class CustomTextField(models.TextField):

    def __init__(self, verbose_name, widget=None, *args, **kwargs):
        self.widget = widget
        self.widget_str=widget.__class__.__name__
        self.verbose_name=verbose_name
        super(CustomTextField, self).__init__(verbose_name, *args, **kwargs)

    def formfield(self, **kwargs):
        defaults = {'widget': CustomTextarea(attrs={'style': '''overflow: hidden;
            word-wrap: break-word; resize: none; width: 200px;'''})}
        if self.widget:
            defaults.update({'widget': self.widget})
        return super(CustomTextField, self).formfield(**defaults)


class CustomCharField(models.CharField):

    def __init__(self, verbose_name, widget=None, *args, **kwargs):
        self.widget = widget
        self.widget_str=widget.__class__.__name__
        self.verbose_name=verbose_name
        super(CustomCharField, self).__init__(verbose_name, *args, **kwargs)

    def formfield(self, **kwargs):
        defaults = {'widget': forms.TextInput(attrs={'style': '''width: 100px;'''})}
        if self.widget:
            defaults.update({'widget': self.widget})
        return super(CustomCharField, self).formfield(**defaults)


from south.modelsinspector import add_introspection_rules
add_introspection_rules([
    (
        [CustomTextField,CustomCharField],
        [['verbose_name',{}]],
        {
            # 'verbose_name':['verbose_name',{}],
            'widget':['widget_str',{'default':None}]
        }
         )], 
        ["^core\.fields\.CustomTextField","^core\.fields\.CustomCharField"])
